﻿
using System;
using System.Web;
using Castle.Core;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;

namespace Family.Data.Factories
{
    //[PerWebRequest]
    public class WebFactory : NHFactory
    {
        private const String SESSION_KEY = "current.session";

        public WebFactory(String configKey) : base(configKey) { }
        
        public static ISession CurrentSession
        {
            get { return (ISession)HttpContext.Current.Items[SESSION_KEY]; }
            set { HttpContext.Current.Items[SESSION_KEY] = value; }
        }

        public override FluentConfiguration GetConfiguration(String configKey)
        {
            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2005.ConnectionString(c => c.FromConnectionStringWithKey(configKey)))
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<WebFactory>());
        }
    }
}
